package com.javaee.test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import com.mysql.jdbc.BlobFromLocator;

public class login {

	public static String url = "https://passport.csdn.net/account/login";
	public static String lt = null;
	public static String execution =null;
	public static String eventId = null;
	private static boolean fetchNecessaryParam(String username,String password) throws IOException {  
	    // 查看CSDN登陆页面源码发现登陆时需要post5个参数  
	    // name、password，另外三个在页面的隐藏域中，a good start  
	    System.out.println("获取必要的登陆信息。。。。。");  
	    // 这样登陆不行，因为第一次需要访问需要拿到上下文context  
	    // Document doc = Jsoup.connect(LOGIN_URL).get();  
	    String html = HttpUtils.sendGet(url);  
	    Document doc = Jsoup.parse(html);  
	    Element form = doc.select(".user-pass").get(0);  
	     lt = form.select("input[name=lt]").get(0).val();  
	     execution = form.select("input[name=execution]").get(0).val();  
	     eventId = form.select("input[name=_eventId]").get(0).val();  
	     System.out.println("获取成功!");  
	     System.out.println("开始登陆。。。。。");  
		    boolean result = false;  
		    List<NameValuePair> nvps = new ArrayList<NameValuePair>();  
		    nvps.add(new BasicNameValuePair("username", username));  
		    nvps.add(new BasicNameValuePair("password", password));  
		    nvps.add(new BasicNameValuePair("lt", lt));  
		    nvps.add(new BasicNameValuePair("execution", execution));  
		    nvps.add(new BasicNameValuePair("_eventId", eventId));  
		    String ret = HttpUtils.sendPost(url, nvps);  
		    if (ret.indexOf("redirect_back") > -1) {  
		        System.out.println("登陆成功。。。。。");  
		        result = true;  
		    } else if (ret.indexOf("登录太频繁") > -1) {  
		        System.out.println("登录太频繁，请稍后再试。。。。。");  
		    } else {  
		        System.out.println("登陆失败。。。。。");  
		    }  
		    return result;  
	     
	}
	
	
	private static boolean mockLogin(String username,String password) {  
	    System.out.println("开始登陆。。。。。");  
	    boolean result = false;  
	    List<NameValuePair> nvps = new ArrayList<NameValuePair>();  
	    nvps.add(new BasicNameValuePair("username", username));  
	    nvps.add(new BasicNameValuePair("password", password));  
	    System.out.println(username+password);
	    nvps.add(new BasicNameValuePair("lt", lt));  
	    nvps.add(new BasicNameValuePair("execution", execution));  
	    nvps.add(new BasicNameValuePair("_eventId", eventId));  
	    String ret = HttpUtils.sendPost(url, nvps);  
	    if (ret.indexOf("redirect_back") > -1) {  
	        System.out.println("登陆成功。。。。。");  
	        result = true;  
	    } else if (ret.indexOf("登录太频繁") > -1) {  
	        System.out.println("登录太频繁，请稍后再试。。。。。");  
	    } else {  
	        System.out.println("登陆失败。。。。。");  
	    }  
	    return result;  
	} 
	
	public static void main(String[] args) throws IOException {
//		login.fetchNecessaryParam();
		boolean flag = login.fetchNecessaryParam("账号", "密码");
		System.out.println(flag);
	}
}
